package com.example.design.adapter.voltage.v3;

import com.example.design.adapter.voltage.v3.adaptee.AC;
import com.example.design.adapter.voltage.v3.adaptee.AC110;
import com.example.design.adapter.voltage.v3.adaptee.AC220;
import com.example.design.adapter.voltage.v3.adapter.ChinaPowerAdapter;
import com.example.design.adapter.voltage.v3.adapter.JapanPowerAdapter;
import com.example.design.adapter.voltage.v3.target.DC5Target;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description 适配器模式测试类
 * 中式变压器和日式变压器
 * @ClassName AdapterTest
 * @Author Administrator
 * @Date 2020/5/13 14:15
 * @Version 1.0.0
 */
public class AdapterTest {
    private List<DC5Target> adapters = new ArrayList<>();

    public AdapterTest(){
        adapters.add(new ChinaPowerAdapter());
        adapters.add(new JapanPowerAdapter());
    }

    public DC5Target getAdapter(AC ac){
        DC5Target adapter = null;
        for (DC5Target ad: adapters) {
            if(ad.support(ac)){
                adapter = ad;
                break;
            }
        }
        if(adapter == null){
            throw new  IllegalArgumentException("没有找到合适的变压适配器");
        }
        return adapter;
    }

    public static void main(String[] args) {
        AdapterTest test = new AdapterTest();
        AC ac110 = new AC110();
        DC5Target adapter = test.getAdapter(ac110);
        adapter.output5V(ac110);

        AC ac220 = new AC220();
        DC5Target adapter1 = test.getAdapter(ac220);
        adapter1.output5V(ac220);
    }

}
